<template>
    <div class="container">
        <div class="main">
            <div class="header flex">
                <div class="headerCard">
                    <p class="headerCardName">库存资产总数量</p>
                    <p class="headerCardtotal">{{ statistics.total }}</p>
                </div>
                <!-- <div class="headerCard">
                    <p class="headerCardName">库存预警条数</p>
                    <p class="headerCardtotal">{{ statistics.warningThresholdTotal }}</p>
                </div> -->
                <div class="headerCard">
                    <p class="headerCardName">出库资产数量</p>
                    <p class="headerCardtotal">{{ statistics.stockOutTotal }}</p>
                </div>
                <div class="headerCard">
                    <p class="headerCardName">入库资产数量</p>
                    <p class="headerCardtotal">{{ statistics.stockInTotal }}</p>
                </div>
                <!-- <div class="headerCard">
                    <p class="headerCardName">物资申领次数</p>
                    <p class="headerCardtotal">{{ statistics.stockOutTimes }}</p>
                </div>
                <div class="headerCard">
                    <p class="headerCardName">物资入库次数</p>
                    <p class="headerCardtotal">{{ statistics.stockInTimes }}</p>
                </div> -->
            </div>
            <div class="row flex">
                <div class="row_l">
                    <div class="row_header">
                        <div class="row_header_l flex" style="margin-bottom: 10px;">
                            <img src="../../assets/DataStatistics_line.png">
                            <p style="color: #666;white-space: nowrap; ">出库</p>
                        </div>
                        <div class="row_header_r flex">
                            <div class="flex" style="margin-right: 10px;">
                                <p style="color: #666;white-space: nowrap;">日期：</p>
                                <el-config-provider :locale="locale">
                                    <el-date-picker 
                                        v-model="date" 
                                        type="daterange" 
                                        range-separator="到"
                                        style="width: 220px;" 
                                        start-placeholder="请选择日期" 
                                        end-placeholder="请选择日期"
                                        @change="changeTime" 
                                        clearable 
                                        :unlink-panels="true"
                                        :default-time="[
                                            new Date(2000, 1, 1, 0, 0, 0),
                                            new Date(2000, 1, 1, 23, 59, 59)
                                        ]"
                                    />
                                </el-config-provider>
                            </div>
                            <div class="flex">
                                <p style="color: #666;white-space: nowrap;">部门：</p>
                                <el-select v-model="form.name" placeholder="请选择部门" @change="changeDepartment"
                                    style="width: 150px" clearable>
                                    <el-option v-for="item in departmentList" :key="item.id" :label="item.name"
                                        :value="item.name" />
                                </el-select>
                            </div>
                            <button class="header_btn" style="white-space: nowrap;"
                                v-loading.fullscreen.lock="fullscreenLoading" @click="exportHandle">导出报表</button>
                        </div>
                        <div style="color: #666;margin-top: 10px;">出库总数量：<span style="color: #000;font-size: 20px;">{{
                        outTotal }}</span></div>
                    </div>
                    <div ref="echart3" class="echart3" id="echart3"></div>
                </div>
                <div class="row_r">
                    <div class="row_header">
                        <div class="row_header_l flex" style="margin-bottom: 10px;">
                            <img src="../../assets/DataStatistics_line.png">
                            <p style="color: #666; ">入库</p>
                        </div>
                        <div class="row_header_r flex">
                            <div class="flex">
                                <p style="white-space: nowrap;">时间：</p>
                                <el-config-provider :locale="locale">
                                    <el-date-picker 
                                        v-model="date1" 
                                        type="daterange" 
                                        range-separator="到"
                                        style="width: 220px;" 
                                        start-placeholder="请选择日期" 
                                        end-placeholder="请选择日期"
                                        @change="changeTime1" 
                                        clearable 
                                        :unlink-panels="true"
                                        :default-time="[
                                            new Date(2000, 1, 1, 0, 0, 0),
                                            new Date(2000, 1, 1, 23, 59, 59)
                                        ]"
                                    />
                                </el-config-provider>
                            </div>
                            <button class="header_btn" style="white-space: nowrap;"
                                v-loading.fullscreen.lock="fullscreenLoading" @click="exportHandle1">导出报表</button>
                        </div>
                        <div style="color: #666;margin-top: 10px;">入库总数量：<span style="color: #000;font-size: 20px;">{{
                                inTotal }}</span></div>
                    </div>
                    <div ref="echart4" class="echart4" id="echart4"></div>
                </div>
            </div>
            <div class="row flex">
                <div class="row_l" style="height:320px;">
                    <div class="row_header flex">
                        <div class="row_header_l flex">
                            <img src="../../assets/DataStatistics_bar.png">
                            <p>资产状态</p>
                        </div>
                        <div class="row_header_r flex">
                        </div>
                    </div>
                    <div ref="echart1" class="echart1" id="echart1"></div>
                </div>
                <div class="row_r" style="height:320px;">
                    <div class="row_header flex">
                        <div class="row_header_l flex">
                            <img src="../../assets/DataStatistics_bar.png">
                            <p>资产动态</p>
                        </div>
                        <div class="row_header_r">
                            <!-- <el-select v-model="value" placeholder="Select" style="width: 240px">
                                <el-option v-for="item in options" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select> -->
                        </div>
                    </div>
                    <div ref="echart1" class="echart2" id="echart2"></div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { httpService } from "@/utils/httpService";
import { formatTime } from "@/utils/dateFormat";
import { ElConfigProvider, ElMessage, ElLoading } from "element-plus";
import { ArrowDown } from "@element-plus/icons-vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import * as echarts from 'echarts';
import { resolveDirective } from "vue";

export default {
    name: "MedicalProductLogin",
    components: { ElMessage, ArrowDown, ElConfigProvider, ElLoading },
    setup() {
        return {
            locale: zhCn,
        };
    },
    data() {
        return {
            page: 1,
            size: 10,
            total: 100,
            statistics: {
                "stockOutTimes": 0,
                "total": 0,
                "stockInTotal": 0,
                "stockInTimes": 0,
                "stockOutTotal": 0,
                "warningThresholdTotal": 0,
            },
            statisticsValue: {
                "total": "库存量",
                "stockInTotal": "入库量",
                "stockOutTotal": "出库量",
                "stockOutTimes": "物资申领次数",
                "stockInTimes": "物资入库次数",
                "warningThresholdTotal": "库存预警条数",
            },
            all: [
                { value: 3, name: '物资申领次数' },
                { value: 5, name: '物资入库次数' },
                { value: 0, name: '库存预警条数' },
            ],
            names: ['物资申领次数', '物资入库次数', '库存预警条数'],
            barNames: [],
            barDatas: [],
            barNames1: [],
            barDatas1: [],
            departmentList: [],
            fullscreenLoading: false,
            form: {
                name: "",
                startTime: "",
                endTime: "",
                startTime1: "",
                endTime1: "",
            },
            date: this.getDefaultDateRange(),
            date1: this.getDefaultDateRange(),

            departmentNames: [],
            departmentDatas: [],
            dateNames: [],
            dateDatas: [],
            outTotal: 0,
            inTotal: 0,


        };
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    computed: {},
    mounted() {
        this.getStatistics()
        this.getBaseDepartmentList()
        this.getStockOut()
        this.getStockIn()

    },

    methods: {
        getStatistics() {
            httpService("/javaApi/materials/statistics", {}, "get").then((data) => {
                if (data.code == 200) {
                    this.statistics = data.object
                    Object.keys(this.statistics).map(key => {
                        if (this.names.includes(this.statisticsValue[key])) {
                            this.barNames.push(this.statisticsValue[key])
                            this.barDatas.push(this.statistics[key])
                        } else {
                            this.barNames1.push(this.statisticsValue[key])
                            this.barDatas1.push(this.statistics[key])
                        }
                    });
                    this.getEchart1()
                    this.getEchart2()
                }
            });
        },
        getStockOut() {
            httpService(
                "/javaApi/stockOut/getData",
                {
                    startTime: this.form.startTime ? this.form.startTime : "",
                    endTime: this.form.endTime ? this.form.endTime : "",
                    department: this.form.name ? this.form.name : ""
                }, "get"
            ).then((data) => {
                if (data) {
                    this.departmentNames = []
                    this.departmentDatas = []
                    this.outTotal = 0
                    for (let i in data) {
                        this.departmentNames.push(i)
                        this.departmentDatas.push(data[i])
                        this.outTotal += Number(data[i])
                    }
                    this.getEchart3()
                }

            });
        },
        getStockIn() {
            httpService(
                "/javaApi/stockIn/getData",
                {
                    startTime: this.form.startTime1 ? this.form.startTime1 : "",
                    endTime: this.form.endTime1 ? this.form.endTime1 : "",
                }, "get"
            ).then((data) => {
                if (data) {
                    this.dateNames = []
                    this.dateDatas = []
                    this.inTotal = 0
                    for (let i in data) {
                        this.dateNames.push(i)
                        this.dateDatas.push(data[i])
                        this.inTotal += Number(data[i])
                    }
                    this.getEchart4()
                }
            });
        },
        exportHandle() {
            this.fullscreenLoading = true
            httpService(
                "/javaApi/stockOut/exportData",
                {
                    startTime: this.form.startTime ? this.form.startTime : "",
                    endTime: this.form.endTime ? this.form.endTime : "",
                }, "get"
            ).then((data) => {
                this.fullscreenLoading = false
                window.open(data.message, "_blank")
                // window.open("http://192.168.9.20" + data.message, "_blank")
            });
        },
        exportHandle1() {
            this.fullscreenLoading = true
            httpService(
                "/javaApi/stockIn/exportData",
                {
                    startTime: this.form.startTime1 ? this.form.startTime1 : "",
                    endTime: this.form.endTime1 ? this.form.endTime1 : "",
                }, "get"
            ).then((data) => {
                this.fullscreenLoading = false
                window.open(data.message, "_blank")
                // window.open("http://192.168.9.20" + data.message, "_blank")
            });
        },

        getBaseDepartmentList() {
            httpService(
                "/javaApi/baseDepartment/list/1/1000",
                {
                    name: this.form.name,
                    type: 1,
                    noCampus: 1,
                }, "get"
            ).then((data) => {
                this.departmentList = data.list;
            });
        },
        changeTime(value) {
            if (value) {
                this.form.startTime = formatTime(new Date(value[0]))
                this.form.endTime = formatTime(new Date(value[1]))
            } else {
                this.form.startTime = ""
                this.form.endTime = ""
            }
            this.getStockOut()
        },
        changeTime1(value) {
            if (value) {
                this.form.startTime1 = formatTime(new Date(value[0]))
                this.form.endTime1 = formatTime(new Date(value[1]))
            } else {
                this.form.startTime1 = ""
                this.form.endTime1 = ""
            }
            this.getStockIn()
        },
        changeDepartment(value) {
            this.getStockOut()
        },

        getEchart1() {

            var chartDom = document.getElementById('echart1');
            if (chartDom == null || !chartDom) {
                chartDom = document.getElementById('echart1');
            }
            console.log(123, chartDom);
            var myChart = echarts.init(chartDom);
            var option;
            option = {
                // color: ['#f44336', '#000', '#fff' ],  
                grid: {
                    left: '10%',    // 距离左边的距离
                    right: '10%',   // 距离右边的距离
                    top: '10%',     // 距离顶部的距离
                    bottom: '10%'   // 距离底部的距离
                },
                xAxis: {
                    type: 'category',
                    data: this.barNames1
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: this.barDatas1,
                        type: 'bar',
                        barWidth: 50,
                        itemStyle: {
                            color: "#6F72FF"
                            // color: function (params) { 
                            //     const colorList = ['#f44336', '#2196f3', '#4caf50' ];
                            //     return colorList[params.dataIndex];
                            // }
                        }
                    }
                ]
            };

            option && myChart.setOption(option);
        },


        getEchart2() {
            var chartDom = document.getElementById('echart2');
            if (chartDom == null || !chartDom) {
                chartDom = document.getElementById('echart2');
            }
            var myChart = echarts.init(chartDom);
            var option;
            option = {
                // color: ['#f44336', '#2196f3', '#4caf50' ],
                grid: {
                    left: '10%',    // 距离左边的距离
                    right: '10%',   // 距离右边的距离
                    top: '10%',     // 距离顶部的距离
                    bottom: '10%'   // 距离底部的距离
                },
                xAxis: {
                    type: 'category',
                    data: this.barNames
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: this.barDatas,
                        type: 'bar',
                        barWidth: 50,
                        itemStyle: {
                            color: "#6F72FF"
                        }
                    }
                ]
            };

            option && myChart.setOption(option);
        },
        getEchart3() {
            var chartDom = document.getElementById('echart3');
            if (chartDom == null || !chartDom) {
                chartDom = document.getElementById('echart3');
            }
            var myChart = echarts.init(chartDom);
            var option;


            option = {
                grid: {
                    left: '10%',    // 距离左边的距离
                    right: '10%',   // 距离右边的距离
                    top: '10%',     // 距离顶部的距离
                    bottom: '10%'   // 距离底部的距离
                },
                xAxis: {
                    type: 'category',
                    data: this.departmentNames
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: this.departmentDatas,
                        type: 'line',
                        smooth: true
                    }
                ]
            };

            option && myChart.setOption(option);
        },
        getEchart4() {
            var chartDom = document.getElementById('echart4');
            if (chartDom == null || !chartDom) {
                chartDom = document.getElementById('echart4');
            }
            var myChart = echarts.init(chartDom);
            var option;


            option = {
                grid: {
                    left: '10%',    // 距离左边的距离
                    right: '10%',   // 距离右边的距离
                    top: '10%',     // 距离顶部的距离
                    bottom: '10%'   // 距离底部的距离
                },
                xAxis: {
                    type: 'category',
                    data: this.dateNames
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: this.dateDatas,
                        type: 'line',
                        smooth: true
                    }
                ]
            };

            option && myChart.setOption(option);
        },
        getDefaultDateRange() {
            const end = new Date();
            const start = new Date();
            start.setMonth(start.getMonth() - 6);
            return [start, end];
        },

    },
};
</script>
<style scoped>
* {
    padding: 0;
    margin: 0;
}

.flex {
    display: flex;
    align-items: center;
}

.container {
    width: calc(100vw - 30px);
    height: calc(100vh - 30px);
    padding: 15px 15px;
    background-color: #EEF7FB;
}

.main {
    width: calc(100% - 30px);
    height: calc(100% - 30px);
    background-color: #fff;
    border-radius: 10px;
    padding: 15px;
    overflow: scroll;
}

.header {
    height: 89px;
    /* background-color: coral; */
}

.headerCard {
    margin-right: 25px;
    border-radius: 10px;
    width: 200px;
    height: 89px;
    background-color: rgb(222, 222, 222);
    background: url(../../assets/DataStatistics_bj.png);
    box-shadow: 3px 3px 2px rgba(0, 0, 0, 0.10);
}

.headerCardName {
    margin: 10px 0 0 10px;
}

.headerCardtotal {
    font-size: 30px;
    line-height: 30px;
    color: rgb(132, 132, 239);
    margin: 10px 0 0 10px;
}

.row {
    border: none;
    margin-top: 20px;
    justify-content: space-between;
}

.row_l {
    width: calc(55% - 30px);
    height: 380px;
    /* margin-right: 15px; */
    border: 1px solid #DFDFDF;
    border-radius: 5px;
}

.row_r {
    width: 45%;
    height: 380px;
    border: 1px solid #DFDFDF;
    border-radius: 5px;
}

.row_header {
    padding: 15px 15px 0 15px;
    justify-content: space-between;
}

.row_header_l {
    font-size: 14px;
    color: #666;
}

.row_header_l img {
    width: 15px;
    height: 15px;
    margin-right: 5px;
}

.echart1 {
    width: 100%;
    height: calc(100% - 50px);
    margin-top: 20px;
}

.echart2 {
    width: 100%;
    height: calc(100% - 50px);
    margin-top: 20px;
}

.echart3 {
    width: 100%;
    height: calc(100% - 150px);
    margin-top: 20px;
}

.echart4 {
    width: 100%;
    height: calc(100% - 150px);
    margin-top: 20px;
}

.header_btn {
    width: 80px;
    padding: 0 10px;
    height: 30px;
    background-color: #6F72FF;
    border-radius: 5px;
    justify-content: center;
    font-size: 14px;
    color: #fff;
    margin-right: 15px;
    cursor: pointer;
    border: none;
    outline: none;
    margin-left: 10px;
}

.el-select-dropdown__item {
    padding-left: 15px;
}
</style>